import logging
import unittest
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from unit_17.Page.Page_login import Page_login
from unit_17.Page.Page1 import Page1
from unit_17.Page.Page2 import Page2
from unit_17.Tools.Tools import Tools
from ddt import ddt, data, unpack, file_data
from unit_17.Data.excel import excel

#测试用例类
excelpath=r'C:\Users\user\PycharmProjects\pythonProject20\unit_17\Data\data.xlsx'
sheetname='Sheet1'
dy=excel(excelpath, sheetname)

@ddt
class Case(unittest.TestCase):
    def setUp(self) -> None:
        Tools().Log()
        self.driver=webdriver.Edge()
        self.driver.maximize_window()
        self.driver.implicitly_wait(6)

    def tearDown(self) -> None:
        sleep(5)
        self.driver.quit()

    # 登录
    @unittest.skip
    @data(*dy.getdatasfromexcel())
    @unpack
    # @file_data('../Data/data.json')
    def test_jiuxian_01(self, username, password, exp, d):
        po=Page_login(self.driver)
        po.jiuxian_luoji01(username, password)
        if d=='T1':
            po.accept()
            sleep(5)
            dy1 = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div/p/span').text
            print(dy1)
            self.assertEqual(dy1, exp)
        elif d=='F1':
            dy2=self.driver.find_element(By.XPATH, '//*[@id="loginform"]/div/div/div/div/div[1]/div[2]/div[2]/div[2]').text
            print(dy2)
            self.assertTrue(exp == dy2)
        else:
            dy3=self.driver.find_element(By.XPATH, '//*[@id="validcaptcha"]/div[3]').text
            print(dy3)
            self.assertIn(exp, dy3)

    @unittest.skip
    def test_jiuxian_02(self):
        po=Page1(self.driver)
        po.open_url('https://www.jiuxian.com/')
        po.jnc()
        po.window_handles()
        sleep(2)
        po.jnc_img()
        po.window_handles()
        sleep(2)
        po.addcar()
        sleep(2)
        dy1=self.driver.find_element(By.XPATH, '//*[@id="u-buy-layId"]/div/div[2]/span').text
        print(dy1)
        self.assertEqual('已成功加入购物车', dy1)
        po.look_car()
        sleep(5)
        dy2=self.driver.find_element(By.XPATH, '//*[@id="cart_container"]/div[2]/div[3]/div[2]/div/div/div[1]/div[2]/div/div[2]/div[1]/a').text
        print(dy2)
        self.assertIn('剑南春', dy2)
        po.login_car()
        sleep(2)
        # po.jiuxian_luoji01()
        po.username('19603907337')
        po.password('Scr12345')
        sleep(20)
        po.login_btn()

        sleep(2)
        dy3=self.driver.find_element(By.XPATH, '//*[@id="cart_container"]/div[3]/div[2]/div[1]/span[2]/em').text
        print(dy3)
        self.assertTrue('￥858.00' == dy3)
        po.jiesuan()
        sleep(2)
        dy4=self.driver.find_element(By.XPATH, '//*[@id="addressListDiv"]/div[1]/h3').text
        print(dy4)
        self.assertEqual('选择收货地址', dy4)
        po.scrollTo()  # 向下滚动
        po.submitOrder()
        sleep(5)
        dy5=self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/ul/li[3]').text
        print(dy5)
        self.assertIn('应付金额', dy5)

    # @unittest.skip
    def test_jiuxian_03(self):
        logging.info('start case')
        po = Page2(self.driver)
        po.jiuxian_luoji01('19603907337', 'Scr12345')  # 登录
        po.accept()
        logging.info('login')
        sleep(5)
        po.my_jiuxian()
        sleep(2)
        po.my_order()
        logging.info('click my_jiuxian-my_order')
        po.window_handles()  # 切换新窗口
        logging.info('window_handles')
        po.scrollTo()  # 向下滚动
        sleep(2)
        po.jiben_info()
        logging.info('click jiben_info')
        po.scrollTo()  # 向下滚动
        sleep(2)
        logging.info('select')
        po.select_province()  # 省
        sleep(1)
        po.select_city()  # 市
        sleep(1)
        po.select_district()  # 区
        sleep(1)
        po.select_street()  # 街道
        sleep(2)
        po.tijiao_btn()
        logging.info('case end')


if __name__ == '__main__':
    unittest.main()
